#PM2.5 leaflets
PM25_1990_leaflet
PM25_2017_leaflet
DALY_1990_leaflet
DALY_2017_leaflet
DALY_change_leaflet
tempdata <- data_healthimp_wide[, c(1, 2, 3, 4, 5)]
tempdata <- tempdata%>%
  pivot_wider(values_from = `ambientpm2.5.dalyper1k`,
              names_from = sex,
              names_glue = "{sex}")
tempdata$DALY <- tempdata$`F` + tempdata$M

tempdata <- tempdata%>%
  select(-c(`F`, M))
tempdata <- tempdata[tempdata$year == 1990 | tempdata$year == 1995 | tempdata$year == 2000| tempdata$year == 2005| tempdata$year == 2010| tempdata$year == 2011| tempdata$year == 2012| tempdata$year == 2013| tempdata$year == 2014| tempdata$year == 2015| tempdata$year == 2016| tempdata$year == 2017, ]

path_bella <- "C:/Users/Bella/Desktop/git/Blog-HealthAndJusticeLeague/data"
data_PM25 <- read_csv(paste0(path_bella,
                             "/Exposure_PM25_air.csv"))

#taking out unnecessary column varaibles
data_PM25 <- data_PM25%>%
  select(COU, Country, Variable, Year, Value)%>%
  rename(country = Country,
         year = Year)

#separate out the variables
data_PM25_wide <- data_PM25%>%
  pivot_wider(values_from = Value,
              names_from = Variable,
              names_glue = "{Variable}")
tempdata2 <- left_join(tempdata, data_PM25_wide, by = c("country", "year"), copy = TRUE)%>%
  select(-c(COU, cou))%>%
  rename(PM25exposure = `Mean population exposure to PM2.5`)

tempdata3 <- tempdata2[, -c(5, 6, 7, 8)]

#tempdata3 <- tempdata3%>%
#  pivot_longer(-c(year, country),
#               names_to = "measure_type", values_to = "value")
tempdata3 <- tempdata3%>%
  filter(is.na(DALY) == FALSE)%>%
  filter(is.na(PM25exposure) == FALSE)


countrylist_index <- floor(runif(10, min = 0, max = nrow(tempdata3)))
countrylist <- rep(c("country"), 10)

for (i in 1:length(countrylist_index)){
  countrylist[i] <- tempdata3$country[countrylist_index[i]]
}

tempdata3 <- subset(tempdata3, country %in% countrylist)


gdaly <- ggplotly(ggplot(data = tempdata3, mapping = aes(x = year, y = DALY, color = country)) +
    geom_line() +
  scale_y_continuous(trans = "log10")+
    labs(
      title = "TITLE",
      subtitle = "1990 to 2017",
      y = "log(DALY)",
      x = "Year")+
    theme_classic())
gdaly
gexposure <- ggplotly(ggplot(data = tempdata3, mapping = aes(x = year, y = PM25exposure, color = country)) +
     geom_line() +
     scale_y_continuous(trans = "log10")+
     labs(
       title = "TITLE",
       subtitle = "1990 to 2017",
       y = "log(population exposure)",
       x = "Year")+
    theme_classic())
 gexposure
#trying out sth different in the meantime
library(ggplot2)
library(gganimate)
library(gifski)


tempdata <- data_healthimp_wide[, c(1, 2, 3, 4, 5)]
tempdata <- tempdata%>%
  pivot_wider(values_from = `ambientpm2.5.dalyper1k`,
              names_from = sex,
              names_glue = "{sex}")
tempdata$DALY <- tempdata$`F` + tempdata$M

tempdata <- tempdata%>%
  select(-c(`F`, M))
tempdata <- tempdata[tempdata$year == 1990 | tempdata$year == 1995 | tempdata$year == 2000| tempdata$year == 2005| tempdata$year == 2010| tempdata$year == 2011| tempdata$year == 2012| tempdata$year == 2013| tempdata$year == 2014| tempdata$year == 2015| tempdata$year == 2016| tempdata$year == 2017, ]

path_bella <- "C:/Users/Bella/Desktop/git/Blog-HealthAndJusticeLeague/data"
data_PM25 <- read_csv(paste0(path_bella,
                             "/Exposure_PM25_air.csv"))

#taking out unnecessary column varaibles
data_PM25 <- data_PM25%>%
  select(COU, Country, Variable, Year, Value)%>%
  rename(country = Country,
         year = Year)

#separate out the variables
data_PM25_wide <- data_PM25%>%
  pivot_wider(values_from = Value,
              names_from = Variable,
              names_glue = "{Variable}")
tempdata2 <- left_join(tempdata, data_PM25_wide, by = c("country", "year"), copy = TRUE)%>%
  select(-c(COU, cou))

g <- ggplot(tempdata2, aes(`Mean population exposure to PM2.5`, DALY, color = country))+
  geom_point(alpha = 0.7, show.legend = FALSE)+
  scale_size(range = c(2, 12))+
  theme_classic()+
  labs(title = 'Year: {round(frame_time, 0)}', x = 'PM 2.5', y = 'DALY per 1K')+
  transition_time(year)+
  ease_aes('linear')

animate(g, duration = 20, fps = 20, width = 400, height = 400, renderer = gifski_renderer())

#anim_save("tempdata2.gif")